package com.springboot.love.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.love.entity.AdminUser;
import com.springboot.love.service.AdminUserService;
import com.springboot.love.service.impl.RedisService;
import com.springboot.love.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * (admin)控制层
 */

@Controller
@RequestMapping("admin")
@Slf4j
public class AdminController {

    @Resource
    AdminUserService adminUserService;
    @Resource
    private RedisService redisService;

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login(HttpSession session, Model model){
        return "admin/login";
    }

    /**
     * 登录
     * @param obj
     * @return
     */
    @RequestMapping(value = "htLogin", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> goLogin(@RequestBody JSONObject obj){
        log.info("登录后台");
        Map<String, Object> map = new HashMap<>();
        String username=obj.getString("username");
        String password=obj.getString("password");
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        Subject currentUser= SecurityUtils.getSubject();
        token.setRememberMe(true);
        try {
            currentUser.login(token);
        } catch (Exception e) {
            token.clear();
            map.put("code",101);
            log.warn("用户 {} 登录失败",username);
            return map;
        }
        AdminUser adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();
        log.info("用户 {} 登录成功",adminUser.getAdminId());
        adminUserService.logadminLogin(adminUser);
        map.put("action","/admin/index");
        map.put("code",0);
        return map;
    }

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpSession session, Model model){
        /*AdminUser user= (AdminUser) SecurityUtils.getSubject().getPrincipal();
        int messageCount = 0;
        try {
            messageCount = redisService.get(user.getAdminId());
        } catch (Exception e) {
        }
        model.addAttribute("messageCount",messageCount);
        model.addAttribute("user",user);
*/
        return "admin/index";
    }

    @RequestMapping(value = "console", method = RequestMethod.GET)
    public String console(){
        return "admin/console";
    }

    @RequestMapping(value = "postAll", method = RequestMethod.GET)
    public String postAll(){
        return "admin/listPost";
    }

}
